package com.xmg.sort;

import java.util.Arrays;
import java.util.HashSet;
import java.util.TreeMap;

public class  QuickSortDemo {
    public static void main(String[] args) {
        int[] arr = {5,3,8,6,2,1,89,34,35,67};
        quickSort2(arr,0,arr.length-1);
        System.out.println(Arrays.toString(arr));
    }

    public static void quickSort(int[] arr,int left,int right){
        if(arr==null || arr.length<=1 || left >= right){
            return;
        }
        int i = left,j = right,pivot = arr[(left+right)/2];
        while (i<=j){
            while (arr[i] < pivot){
                i++;
            }
            while (arr[j] > pivot){
                j--;
            }
            if(i<j){
                int t = arr[i];
                arr[i] = arr[j];
                arr[j] = t;
                i++;
                j--;
            }else if(i==j){
                i++;
            }
        }
        quickSort(arr,left,j);
        quickSort(arr,i,right);
    }











    public static void quickSort2(int[] arr,int left,int right){
        if(arr == null || arr.length==0){
            return;
        }
        int i = left;
        int j = right;
        int pivot = (left + right) /2;
        while (i<j){
            while (arr[i]<arr[pivot]){
                i++;
            }
            while (arr[j]>arr[pivot]){
                j--;
            }
            if(arr[i]>arr[j]){
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
                i++;
                j--;
            }else if(arr[i]==arr[j]){
                i++;
            }

            quickSort2(arr,left,j);
            quickSort2(arr,i,right);
        }
    }
}
